package com.app.milkteaapp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.app.milkteaapp.bean.FindUserGsonBean;
import com.app.milkteaapp.R;
import com.app.milkteaapp.utils.GsonUtils;
import com.app.milkteaapp.utils.OkHttpUtils;
import com.app.milkteaapp.utils.MyTools;
import com.app.milkteaapp.utils.SpUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BalanceActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_money;
    private Button btn_no;

    //余额
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance);
        initView();
        //拿用户id
       int  userid =  SpUtils.getUserBean(this).getId();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id",userid);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        OkHttpUtils.MyCall(MyTools.URL+"/user/findById",jsonObject.toString()).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "网络炸了", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //gson解析
                String string = response.body().string();

                final FindUserGsonBean findUserGsonBean = GsonUtils.fromJson(string,FindUserGsonBean.class);

                if(findUserGsonBean.isSuccess()){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_money.setText(findUserGsonBean.getData().getMoney()+"");
                        }
                    });
                }
            }
        });
    }

    private void initView() {
        tv_money = (TextView) findViewById(R.id.tv_money);
        btn_no = (Button) findViewById(R.id.btn_no);

        btn_no.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_no:
                    finish();
                break;
        }
    }
}
